// This began from the book _Java in a Nutshell_ by David Flanagan.
// but I doubt they would claim it, now.
// Written by md1i@virginia.edu and ajd2m@virginia.edu
//
// This applet shows two sine waves of different wavelengths interfering
// as they travel at different velocities.  There is a scrollbar and a
// textbox which change the velocity of the longer wavelength.

import java.applet.*;
import java.awt.*;

public class GroupVelocity extends Applet {
	// This is an animation derived from Canvas.  It shows the waves.
	WavePacketCanvas sines;
	TextSlide slideField;  // Shows the value of the velocity.
	TextSlide freqField;  // Shows the value of the frequency.
	double minVal = 0.0, maxVal = 2.0;  // Min and max values for velocity from scrollbar.

	// Initializtion called bye the applet container.
	public void init() {
		// We use a very simple layout manager.
		this.setLayout(new BorderLayout(15,15));
		Panel p = new Panel();
		p.setLayout(new GridLayout(2,1,0,4));
		// Create a text field and add it to the layout.
		slideField = new TextSlide(this,"Group Velocity: ",minVal,maxVal);
		p.add(slideField);
		freqField = new TextSlide(this,"Frequency: ",5,25);
		p.add(freqField);
		add("South",p);
		// This object is a homegrown class, but it can be added b/c it is also a canvas.
		sines = new WavePacketCanvas();
		this.add("Center",sines);
		// Set the value of the slider and scrollbar.
		slideField.setValue(0.6);
		freqField.setValue(12);
		// You have to call validate to convince the canvas to adjust its layout.
		validate();
	}

	// Start the animation.  Called by applet's environment.  The applet calls the sines thread.
	public void start() { 
		sines.enable();
	}

	// Stop it.
	public void stop() { 
		sines.disable();
	}

	public void changeParam(TextSlide which) {
		if (which == slideField) {
			sines.setVelocity(slideField.getValue());
		} else if (which == freqField) {
			sines.setFrequency(freqField.getValue());
		}
	}

	// This method draws the background and  text at its current position.
	// There is nothing to do b/c each component draws itself.
	public void paint(Graphics g) {
		;
	}

	// Keep a main around in case we want to debug without an html page.
	// This could be greatly improved or deleted.  Either is appropriate.ff
	public static void main(String args[]) {
		Applet applet = new GroupVelocity();
		Frame f = new Frame("GroupVelocity");
		f.add("Center",applet);
		applet.init();
		applet.start();
	}

}
